package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.StoreImageDAO;
import com.xinghou.platform.model.main.StoreImage;

/**
 * @author liuwei.wen
 *
 */
@Service
public class StoreImageDAOImpl {/*

	@Resource
	private JdbcTemplate jdbcTemplate;

	//查询门店 999 有多条数据 取第一条
	public StoreImage getStoreImageList(Integer storeId, Integer imageTypeId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT Image_id,Store_id,File_name,Description,Uri,Image_type_id, City_id, Store_type_id, Status  FROM store_image where Store_id=? and Image_type_id=?";
		List<StoreImage> storeImages=jdbcTemplate.query(sql,new Object[] {storeId,imageTypeId},new StoreImageMapper());
		CustomerContextHolder.clearCustomerType();
		if(storeImages.size()>0){
			return storeImages.get(0);
		}
		
		return null;
	}
	
	public List<StoreImage> getStoreImageList(Integer storeId, Integer imageTypeId,int limit) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT Image_id,Store_id,File_name,Description,Uri,Image_type_id, City_id, Store_type_id, Status FROM store_image where Store_id=? ";
				
		if(imageTypeId!=2){
			sql = sql + "and Image_type_id="+imageTypeId+" ";
		}
		
		sql = sql + "ORDER BY Image_id ASC";
		
		List<StoreImage> storeImages;
		if(limit == -1){
			storeImages=jdbcTemplate.query(sql,new Object[] {storeId},new StoreImageMapper());
		}else{
			sql = sql + " LIMIT ?"; 
			storeImages=jdbcTemplate.query(sql,new Object[] {storeId,limit},new StoreImageMapper());
		}
		CustomerContextHolder.clearCustomerType();
		return storeImages;
	}
	
	private static final class StoreImageMapper implements RowMapper<StoreImage> {
		public StoreImage mapRow(ResultSet rs, int rowNum) throws SQLException {
			StoreImage storeImage = new StoreImage();
			storeImage.setDescription(rs.getString("Description"));
			storeImage.setFileName(rs.getString("File_name"));
			storeImage.setImageId(rs.getInt("Image_id"));
			storeImage.setImageTypeId(rs.getInt("Image_type_id"));
			storeImage.setStoreId(rs.getInt("Store_id"));
			storeImage.setUri(rs.getString("Uri"));
			//storeImage.setStoreImageurl(rs.getString("StorImageurl"));
			storeImage.setCityId(rs.getInt("City_id"));
			storeImage.setStoreTypeId(rs.getInt("Store_type_id"));
			storeImage.setStatus(rs.getString("Status"));
			return storeImage;
		}
	}
	
	@Override
	public int saveStroeImage(StoreImage storeImage) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		String sql="INSERT INTO store_image (Store_id,File_name,Description,Uri,Image_type_id, City_id, Store_type_id, Status) VALUES (?,?,?,?,?,?,?,?)";
		int ret=jdbcTemplate.save(sql,new Object[] {storeImage.getStoreId(),storeImage.getFileName(),storeImage.getDescription(),storeImage.getUri(),
				storeImage.getImageTypeId(),storeImage.getCityId(),storeImage.getStoreTypeId(),storeImage.getStatus()});
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public StoreImage getFirstStoreImage(int storeid)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT Image_id,Store_id,File_name,Description,Uri,Image_type_id, City_id, Store_type_id, Status  FROM store_image where Store_id=? order by Createtime limit 1";
		StoreImage storeImage=jdbcTemplate.queryForObject(sql,new Object[] {storeid},new StoreImageMapper());
		CustomerContextHolder.clearCustomerType();
		return storeImage;
	}*/
}
